using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using UnityEngine;

namespace BitMango
{
	public class WalletInstance
	{
		public WalletInstance(UncleBillContext cntx)
		{
			this._cntx = cntx;
			this._loaded = false;
			foreach (InventoryItem inventoryItem in this._cntx.inventoryItems)
			{
				if (inventoryItem.consumable)
				{
					this._items.Add(new InventoryItem(inventoryItem.id));
				}
			}
		}

		public bool loaded
		{
			get
			{
				return this._loaded;
			}
		}

		public List<InventoryItem> inventory
		{
			get
			{
				if (!this._loaded)
				{
					UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
				}
				return this._items;
			}
		}

		private void loadInternal(ArrayList newInventory)
		{
			if (newInventory != null)
			{
				int i = 0;
				while (i < newInventory.Count)
				{
					string text = newInventory[i] as string;
					int count = Convert.ToInt32(newInventory[i + 1]);
					InventoryItem inventoryItem = this.Get(text);
					if (inventoryItem != null)
					{
						goto IL_79;
					}
					InventoryItem inventoryItemByID = this._cntx.GetInventoryItemByID(text);
					if (inventoryItemByID != null)
					{
						inventoryItem = new InventoryItem(text);
						this._items.Add(inventoryItem);
						goto IL_79;
					}
					UnityEngine.Debug.LogWarning("Wallet.Load ItemID Not Found : " + text);
					IL_87:
					i += 2;
					continue;
					IL_79:
					inventoryItem.count = count;
					Wallet.FireItemChangedEvent(inventoryItem, count);
					goto IL_87;
				}
			}
			else
			{
				UnityEngine.Debug.LogWarning("Error loading wallet_inventory");
			}
			Wallet.FireLoadingDoneEvent();
		}

		public bool Load()
		{
			this._loaded = true;
			string @string = PlayerPrefs.GetString("wallet_inventory");
			ArrayList newInventory = @string.arrayListFromJson();
			this.loadInternal(newInventory);
			return this._loaded;
		}

		public bool Save()
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Can't Save non initialized Wallet");
				return false;
			}
			ArrayList arrayList = new ArrayList();
			foreach (InventoryItem inventoryItem in this._items)
			{
				arrayList.Add(inventoryItem.id);
				arrayList.Add(inventoryItem.count);
			}
			string value = MiniJSON.jsonEncode(arrayList);
			PlayerPrefs.SetString("wallet_inventory", value);
			PlayerPrefs.Save();
			return true;
		}

		public InventoryItem Get(string id)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			return this._items.Find((InventoryItem item) => item.id == id);
		}

		public int GetItemCount(string id)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			InventoryItem inventoryItem = this._items.Find((InventoryItem item) => item.id == id);
			if (inventoryItem == null)
			{
				return 0;
			}
			return inventoryItem.count;
		}

		public void Gain(ShopItem newItem, string where)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			UnityEngine.Debug.Log("wallet.Gain : " + newItem.id);
			foreach (ShopItem.Package package in newItem.rewards)
			{
				InventoryItem inventoryItem = package.GetItem();
				if (inventoryItem == null)
				{
					inventoryItem = new InventoryItem(package.itemId);
					this._items.Add(inventoryItem);
				}
				if (package.IsInfinite)
				{
					inventoryItem.count = package.count;
				}
				else if (!inventoryItem.IsInfinite)
				{
					inventoryItem.count += package.count;
				}
				Wallet.FireItemChangedEvent(inventoryItem, package.count);
				if (where == "iap" || where == "recovery")
				{
					DWATTHelper.logItemBuy(inventoryItem.id, package.count, inventoryItem.count, where);
				}
				else if (where != "restore")
				{
					DWATTHelper.logItemEarn(inventoryItem.id, package.count, inventoryItem.count, where);
				}
			}
		}

		public void Refund(ShopItem newItem)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			UnityEngine.Debug.Log("wallet.Refund : " + newItem.id);
			foreach (ShopItem.Package package in newItem.rewards)
			{
				InventoryItem item = package.GetItem();
				if (item != null)
				{
					if (package.IsInfinite)
					{
						InventoryItem inventoryItemByID = this._cntx.GetInventoryItemByID(package.itemId);
						if (inventoryItemByID != null)
						{
							item.count = inventoryItemByID.count;
						}
					}
					else if (!item.IsInfinite)
					{
						item.count = Mathf.Max(item.count - package.count, 0);
					}
					Wallet.FireItemChangedEvent(item, -package.count);
				}
			}
		}

		public void Gain(string inventoryItemId, int count, string where)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			UnityEngine.Debug.Log(string.Concat(new object[]
			{
				"wallet.Gain : ",
				inventoryItemId,
				"  count:",
				count
			}));
			InventoryItem inventoryItem = this.Get(inventoryItemId);
			if (inventoryItem == null)
			{
				inventoryItem = new InventoryItem(inventoryItemId);
				this._items.Add(inventoryItem);
			}
			if (!inventoryItem.IsInfinite)
			{
				inventoryItem.count += count;
			}
			Wallet.FireItemChangedEvent(inventoryItem, count);
			if (where == "iap")
			{
				DWATTHelper.logItemBuy(inventoryItem.id, count, inventoryItem.count, where);
			}
			else if (where != "restore")
			{
				DWATTHelper.logItemEarn(inventoryItem.id, count, inventoryItem.count, where);
			}
		}

		public bool Use(string id, int count, string where)
		{
			if (!this._loaded)
			{
				UnityEngine.Debug.LogError("Accessing Unloaded Wallet");
			}
			InventoryItem inventoryItem = this.Get(id);
			if (inventoryItem == null)
			{
				UnityEngine.Debug.LogError("Wallet Error : Use of non-existing item [" + id + "]");
				return false;
			}
			if (inventoryItem.count == 0)
			{
				return false;
			}
			if (inventoryItem.IsInfinite)
			{
				return true;
			}
			inventoryItem.count -= count;
			inventoryItem.count = Mathf.Max(0, inventoryItem.count);
			Wallet.FireItemChangedEvent(inventoryItem, -count);
			Debugger.Assert(where != "iap", "WalletInstance.Use where is iap!!");
			DWATTHelper.logItemUse(inventoryItem.id, count, inventoryItem.count, where);
			return true;
		}

		public bool hasNonConsumable
		{
			get
			{
				foreach (InventoryItem inventoryItem in this._items)
				{
					if (!inventoryItem.consumable && inventoryItem.count > 0)
					{
						return true;
					}
				}
				return false;
			}
		}

		public void MigrationFromBalanceToInventory(string inventoryItemId)
		{
			InventoryItem inventoryItem = this.Get(inventoryItemId);
			Debugger.Assert(inventoryItem != null, "MigrationFromBalanceToInventory : you must prepare Inventory Item at Context _ Id : " + inventoryItemId);
			if (inventoryItem.count != 0)
			{
				Debugger.Log("MigrationFromBalanceToInventory : inventoryItemId Already exist _ now count is : " + inventoryItem.count);
			}
			else
			{
				Debugger.Assert(inventoryItem.consumable, "MigrationFromBalaceToInventory : plz set GameMoney's consumable to true ");
				inventoryItem.count += PlayerPrefs.GetInt("wallet_balance");
			}
		}

		private UncleBillContext _cntx;

		private List<InventoryItem> _items = new List<InventoryItem>();

		private bool _loaded;
	}
}
